﻿using System;
using System.ServiceModel.Syndication;
using System.Text;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Net;

namespace DataService.Model
{
    [DataContract]
    public class SyndicationItemBase : SyndicationItem
    {
        private SyndicationItem _baseItem;
		private string _imageUrl;
		private string _description;
		private string _publisher;
		private string _sourceUrl;
		private string _source;
		private string _sumaryText;
		private DateTimeOffset _publishDate;
		private List<Uri> _links;
		private string _title;
		protected SyndicationItem BaseItem
		{
			get
			{
				return this._baseItem;
			}
			set
			{
				this._baseItem = value;
			}
		}
		[DataMember]
		public string ImageSrc
		{
			get
			{
				return this._imageUrl;
			}
			set
			{
				this._imageUrl = value;
			}
		}
		[DataMember]
		public string Description
		{
			get
			{
				return this._description;
			}
			set
			{
				this._description = value;
			}
		}
		[DataMember]
		public string SummaryText
		{
			get
			{
				return this._sumaryText;
			}
			set
			{
				this._sumaryText = value;
			}
		}
		[DataMember]
		public string TitleText
		{
			get
			{
				return this._title;
			}
			set
			{
				this._title = value;
			}
		}
		[DataMember]
		public string Publisher
		{
			get
			{
				return this._publisher;
			}
			set
			{
				this._publisher = value;
			}
		}
		[DataMember]
		public DateTimeOffset PubDate
		{
			get
			{
				return this._publishDate;
			}
			set
			{
				this._publishDate = value;
			}
		}
		public string PublishedDateString
		{
			get
			{
                return string.Format("posted {0} at {1}", this.PublishDate.LocalDateTime.ToString("MMM dd yyyy"), this.PubDate.LocalDateTime.ToShortTimeString());
			}
		}
		[DataMember]
		public string Source
		{
			get
			{
				return this._source;
			}
			set
			{
				this._source = value;
			}
		}
		[DataMember]
		public string SourceUrl
		{
			get
			{
				return this._sourceUrl;
			}
			set
			{
				this._sourceUrl = value;
			}
		}
		[DataMember]
		public List<Uri> Links
		{
			get
			{
				return this._links;
			}
			set
			{
				this._links = value;
			}
		}

        public SyndicationItemBase(SyndicationItem baseItem)
		{
			this._baseItem = baseItem;
			this._sumaryText = ((baseItem != null) ? this.DecodeString(baseItem.Summary.Text) : string.Empty);
			this._publishDate = baseItem.PublishDate;
			this._title = this.DecodeString(baseItem.Title.Text);
			this._links = new List<Uri>();
			using (IEnumerator<SyndicationLink> enumerator = baseItem.Links.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
                    SyndicationLink current = enumerator.Current;
					this._links.Add(current.Uri);
				}
			}
			this.OnLoad();
		}
		protected virtual void OnLoad()
		{
			this.ParseSummary(this.SummaryText);
			this.ParsePublisher();
		}
		protected virtual void ParseSummary(string summaryText)
		{
			int num = summaryText.IndexOf("<img");
			int num2 = summaryText.IndexOf("src=", num) + 5;
			int num3 = summaryText.IndexOf("\"", num2);
			this.ImageSrc = summaryText.Substring(num2, num3 - num2);
			if (string.IsNullOrEmpty(this.ImageSrc))
			{
				this.ImageSrc = "Images\\SmallLogo.png";
			}
			int num4;
			if (summaryText.StartsWith("<div"))
			{
				num4 = summaryText.IndexOf("/div", num3) + 5;
			}
			else
			{
				num4 = summaryText.IndexOf("/a", num3) + 3;
			}
			StringBuilder stringBuilder = new StringBuilder();
			bool flag = false;
			int i;
			for (i = num4; i < summaryText.Length- 1; i++)
			{
				if (summaryText[i] == '<')
				{
					flag = true;
					if (summaryText[i+1] == 'p')
					{
						break;
					}
				}
				else
				{
					if (summaryText[i] == '>')
					{
						flag = false;
					}
					else
					{
						if (!flag)
						{
							stringBuilder.Append(summaryText[i]);
						}
					}
				}
			}
			this.Description = this.DecodeString(stringBuilder.ToString());
			int num5 = summaryText.IndexOf("alt=\"source\"", i);
			if (num5 > 0)
			{
				num5 = summaryText.IndexOf("href", num5) + 6;
				int num6 = summaryText.IndexOf("\"", num5);
				this.SourceUrl = summaryText.Substring(num5, num6 - num5);
				num5 = num6 + 2;
				num6 = summaryText.IndexOf("</a>", num6);
				this.Source = summaryText.Substring(num5, num6 - num5);
			}
		}
		protected virtual void ParsePublisher()
		{
			string text = (this.BaseItem.ElementExtensions.Count > 1) ? this.BaseItem.ElementExtensions[1].GetReader().ReadInnerXml() : string.Empty;
			StringBuilder stringBuilder = new StringBuilder(text);
			stringBuilder.Replace("<![CDATA[", "");
			stringBuilder.Replace("]]>", "");
			this.Publisher = this.DecodeString(stringBuilder.ToString());
		}
		protected string DecodeString(string encodedString)
		{
			return HttpUtility.HtmlDecode(encodedString);
		}
    }
}
